#include "themebutton.h"
#include <QVBoxLayout>
#include <QLabel>
#include <QDebug>
#include <QPainter>

ThemeButton::ThemeButton(const QString &name, QWidget *parent):
    QPushButton(parent),
    themename(name)
{
    mIconLabel = new ThemeIconLabel(this);
    initUi();
    setConnect();
}

ThemeButton::ThemeButton(const QString &name, const QPixmap &pixmap, QWidget *parent):
    QPushButton(parent),
    themename(name)
{
    mIconLabel = new ThemeIconLabel(pixmap, this);
    initUi();
    setConnect();
}


ThemeButton::ThemeButton(QString name, QStringList iStringList, QWidget *parent):
    themename(name),
    QPushButton(parent)
{
    mIconLabel = new ThemeIconLabel( iStringList, this);
    initUi();
    setConnect();
}

ThemeButton::ThemeButton(QString name, QList<QPixmap> iconList, QWidget *parent):
    themename(name),
    QPushButton(parent)
{
    mIconLabel = new ThemeIconLabel( iconList, this);
    initUi();
    setConnect();
}

void ThemeButton::initUi()
{
    clickedBtnStyle = getStyle(2, mRadius);
    enterBtnStyle = getStyle(1, mRadius);
    leaveBtnStyle = getStyle(0, mRadius);

    setCheckable(true);
    setFixedSize(mWidth, mHeigh);
    QVBoxLayout *baseVerLayout = new QVBoxLayout(this);
    baseVerLayout->setSpacing(8);
    baseVerLayout->setMargin(0);

    mIconLabel->setFixedSize(QSize(mWidth, mHeigh - 32));
    mIconLabel->setAttribute(Qt::WA_DeleteOnClose);

    QHBoxLayout *bottomHorLayout = new QHBoxLayout;
    bottomHorLayout->setSpacing(8);
    bottomHorLayout->setMargin(0);

    mNameLabel = new QLabel(this);
    mNameLabel->setText(themename);

    bottomHorLayout->addStretch();
    bottomHorLayout->setContentsMargins(0, 16, 0, 0);
    bottomHorLayout->addWidget(mNameLabel);
    bottomHorLayout->addStretch();

    baseVerLayout->addWidget(mIconLabel);
    baseVerLayout->addLayout(bottomHorLayout);

    this->setLayout(baseVerLayout);
}

void ThemeButton::setConnect()
{
    connect(this, &ThemeButton::clicked, this, [=]() {
        mIconLabel->setStyleSheet(clickedBtnStyle);
    });

    connect(this, &ThemeButton::mBtnCheckedSignals, [=](bool status) {
        this->setChecked(status);
        if (!status)
            mIconLabel->setStyleSheet(leaveBtnStyle);
    });

    connect(mIconLabel,&ThemeIconLabel::enterWidget,[=](){
        if (!this->isChecked()) {
            mIconLabel->setStyleSheet(enterBtnStyle);
        }
    });

    connect(mIconLabel,&ThemeIconLabel::leaveWidget,[=](){
        if (!this->isChecked()) {
            mIconLabel->setStyleSheet(leaveBtnStyle);
        }
    });
}

void ThemeButton::setRadius(int radius)
{
    mRadius = radius;
    mIconLabel->setRadius(radius);

    clickedBtnStyle = getStyle(2, mRadius);
    enterBtnStyle = getStyle(1, mRadius);
    leaveBtnStyle = getStyle(0, mRadius);
}

void ThemeButton::setRect(QSize size)
{
    mWidth = size.width();
    mHeigh = size.height();
    setFixedSize(size);
    mIconLabel->setFixedSize(QSize(mWidth, mHeigh - 32));

}

void ThemeButton::setPixmap(const QPixmap &pixmap)
{    
    mIconLabel->setIcon(pixmap);
}

void ThemeButton::setBtnClicked(bool status)
{
    this->setChecked(status);
    if (!this->isChecked()) {
        mIconLabel->setStyleSheet(leaveBtnStyle);
    } else {
        mIconLabel->setStyleSheet(clickedBtnStyle);
    }
}

QString ThemeButton::getStyle(int border, int radius)
{
    return QString("ThemeIconLabel{border-width: %1px;border-style: solid;border-radius: %2px}").arg(border).arg(radius);
}

void ThemeButton::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing);  // 反锯齿;
    painter.setClipping(true);
    painter.setPen(Qt::transparent);

    painter.drawRoundedRect(this->rect(), mRadius, mRadius);
}
